<?php
namespace app\webadmin\controller;
use app\common\controller\Webadmin ; 

class Art extends Webadmin
{
    


    //控制器初始化
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Art');
        $this->model_cate = model('Cate');
    }
    /**
     * 配置管理
     * @author  lrxin <lrxin@msn.com>
     */
    public function index()
    {
     
        /* 查询条件初始化 */
        $map= array();
        $search_cate  = input('search_cate');
        $search_title  = input('search_title');

        $ids  = $this->model_cate->tree->getids($search_cate);

        if( $search_cate ){  $map['cid'] =  ['in',$ids] ;}
        if( $search_title ){ $map['title'] =  ['like', $search_title ]  ;}



        $cate_list = model('cate')->getTreeList();
        $list = $this->model->where($map)->order(' sort asc , sort_time desc ')->paginate(1,false,['query' => request()->param()] );
        // 记录当前列表页的cookie
        //Cookie('__forward__', $_SERVER['REQUEST_URI']);
        $data = array(
            'list'        => $list,
            'cate_list'        => $cate_list,

            'search_cate'        => $search_cate,
            'search_title'        => $search_title,
        );
        $this->assign( $data );
        $this->setMeta('文章管理');
        return view('index');
    }


    /**
     * 添加 ， 修改 ， 保存
     * @return [type] [description]
     */
    public function edit( $id= 0 ){
        if(IS_AJAX){
            $data   = $this->request->post();

            $result = $this->model->allowField(true)->isUpdate(!!$data['id'])->validate(true)->save($data);
            if (false === $result) {
                return $this->error( '错误：'. $this->model->getError() );
            } else {
                return ret( 0 , '更新成功');
            }
  
        }else{


            $info = array();
            /* 获取数据 */
            $info = $this->model->where('id',$id)->find();

            if(!$info){

                $info['title'] = '';
                $info['title2'] = '';

                $info['writer'] = '';
                $info['source'] = '';

                $info['update_time'] = date('Y-m-d H:i:s');
                $info['sort_day'] = 0;

                $info['type'] = 1;

                $info['display'] = 1;
                $info['click'] = rand(50,100);
            }

            $cate_list = model('cate')->getTreeList();

            $assign = array(
                'info' => $info,
                'cate_list' => $cate_list,
            );

            $this->assign($assign);
            $this->setMeta('编辑配置');
            return view();

        }

    }
    




    /**
     * 删除配置  一个删除 和 多个删除
     *  
     */
    public function del() {
        $id = array_unique((array) input('id/a'));

        if (empty($id)) {
            return $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id));


        if ( $this->model->where($map)->delete() ) {
            return $this->success('删除成功！');   //$msg = '', $url = null, $data = '', $wait = 3, array $header = []
        } else {
            return $this->error('删除失败！' );    // $msg = '', $url = null, $data = '', $wait = 3, array $header = []
        }
    }


    

    // /**
    //  * ajax 读取一条记录
    //  * @param  integer $dept_id [description]
    //  * @return [json]           [description]
    //  */
    // public function get( $dept_id = 0 ){
    //     //$this->requestInfo();
    //     if( ! $this->request->isAjax() ){
    //         return 'not ajax!!!' ;
    //     }
    //     $rs = $this->Dept->one($dept_id);
    //     return json($rs);

    // }

    // /**
    //  * 添加 or  修改
    //  * @return [type]           [description]
    //  */
    // public function save( ){

    //     if(!IS_AJAX){ return 'not ajax!!!' ;}

    //     if( false ===  $this->Dept->change() ){
    //         return json(['ret'=> 1 , 'msg'=> $this->Dept -> getError() , 'data' => null ]); 
    //     }
    //     return json(['ret'=> 0 , 'msg'=> 'ok' , 'data' => null ]); 
    // }


    // /**
    //  * [删除 ajax]
    //  * @return [type] [description]
    //  */
    // public function delete(){
    //         if(!IS_AJAX){ return 'not ajax!!!' ;}

    //         $id =  input('post.dept_id/d' , ''  ); //  ( '数据' ， ‘默认值’ ，过滤函数 )

    //         //是否允许删除
    //         if (false === $this->Dept->isAllowDelete( $id ) ){
    //             return json(['ret'=> 1 , 'msg'=> $this->Dept -> getError() , 'data' => null ]); 
    //         }

    //         // $rs = $this->Dept->where('dept_id' , $id )->delete();
    //         $rs = $this->Dept->destroy( $id ); 
    //         return json(['ret'=> 0 , 'msg'=> 'ok' , 'data' => null ]); 

    // }




    


}
